package com.three.crm.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.three.crm.util.AppManager;
import com.three.crm.util.ToastUtils;

public abstract class BaseFragment extends Fragment implements View.OnClickListener{
    //获取TAG的fragment名称
    protected final String TAG = this.getClass().getSimpleName();
    private View contentView;
    public Context context;
    private LoadingPopupView loadingView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            initArguments(getArguments());
        }
    }

    @Override
    public void onAttach(@NonNull Context mcontext) {
        super.onAttach(context);
        context = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(bindLayout(),container,false);
        loadingView = (LoadingPopupView) new XPopup.Builder(context)
                .asLoading("正在初始化...");
        initViews(contentView,savedInstanceState);
        initListener();
        initData(context);
        return contentView;
    }

    protected void showLoading(String text){
        if (loadingView != null){
            loadingView.setTitle(text);
            loadingView.show();
        }
    }

    protected void hideLoading(){
        if (loadingView != null){
            loadingView.smartDismiss();
        }
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T $(int resId) {
        return (T) contentView.findViewById(resId);
    }

    protected void showToast(String msg){
        ToastUtils.showToast(msg);
    }

    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }

    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    protected abstract void initArguments(Bundle bundle);

    /**
     * 初始化布局
     * @return 布局id
     */
    protected abstract int bindLayout();

    /**
     * 初始化控件
     * @param view 布局view
     */
    protected abstract void initViews(View view,Bundle savedInstanceState);

    public abstract void initListener();
    public abstract void widgetClick(View v);

    /**
     * 初始化，绑定数据
     * @param context 上下文
     */
    protected abstract void initData(Context context);

    protected long getTimestamp() {
        return AppManager.getTimesTamp();
    }

    @Override
    public void onClick(View v) {
        if (fastClick())
            widgetClick(v);
    }

    private boolean fastClick() {
        long lastClick = 0;
        if (System.currentTimeMillis() - lastClick <= 1000) {
            return false;
        }
        lastClick = System.currentTimeMillis();
        return true;
    }

    /**
     * 保证同一按钮在1秒内只响应一次点击事件
     */
    public abstract class OnSingleClickListener implements View.OnClickListener {
        //两次点击按钮的最小间隔，目前为1000
        private static final int MIN_CLICK_DELAY_TIME = 1000;
        private long lastClickTime;

        public abstract void onSingleClick(View view);

        @Override
        public void onClick(View v) {
            long curClickTime = System.currentTimeMillis();
            if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
                lastClickTime = curClickTime;
                onSingleClick(v);
            }
        }
    }

    /**
     * 同一按钮在短时间内可重复响应点击事件
     */
    public abstract class OnMultiClickListener implements View.OnClickListener {
        public abstract void onMultiClick(View view);

        @Override
        public void onClick(View v) {
            onMultiClick(v);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
